begintownscript;

variables;
short bmessage,pcs,seated,leadchar,rctx,rcty,stepsnd,sld_typ,telep_ok;
string custom_m;

body;

beginstate INIT_STATE;
	turn_off_training(1);

	if(get_flag(15,1) < 3){
		rctx = 23;
		while(rctx <= 41){
			rcty = 33;
			while(rcty <= 47){
				set_floor(rctx,rcty,37);
				if(get_ran(1,0,9) == 0)
					set_floor(rctx,rcty,38);
				if(get_ran(1,0,9) == 0)
					set_floor(rctx,rcty,39);
				if(get_ran(1,0,10) == 0)
					set_floor(rctx,rcty,40);

				rcty = (rcty + 1);
			}
			rctx = (rctx + 1);
		}
		set_terrain(24,34,0);
		set_terrain(24,36,0);
		set_terrain(24,38,0);
		set_terrain(24,42,0);
		set_terrain(24,44,0);
		set_terrain(24,46,0);
		set_terrain(26,33,0);
		set_terrain(26,34,0);
		set_terrain(27,36,0);
		set_terrain(28,33,0);
		set_terrain(28,34,0);
		set_terrain(28,37,0);
		set_terrain(28,40,0);
		set_terrain(30,33,0);
		set_terrain(34,33,0);
		set_terrain(36,33,0);
		set_terrain(36,34,0);
		set_terrain(36,37,0);
		set_terrain(36,40,0);
		set_terrain(37,36,0);
		set_terrain(38,33,0);
		set_terrain(38,34,0);
		set_terrain(40,34,0);
		set_terrain(40,36,0);
		set_terrain(40,38,0);
		set_terrain(40,42,0);
		set_terrain(40,44,0);
		set_terrain(40,46,0);
	}

	set_incidental_sound(1);
	if(get_flag(15,1) < 2){
		march_party(63,63);
		march_party(63,63);
		march_party(63,63);
		march_party(63,63);

		march_party(32,63);
		force_view_center(32,63);
		if(party_size() > 1){
			march_party(32,62);
			force_view_center(32,62);
		}
		if(party_size() > 2){
			march_party(32,61);
			force_view_center(32,61);
		}
		if(party_size() > 3){
			march_party(32,60);
			force_view_center(32,60);
		}
		set_character_facing(1000,0);
		force_instant_terrain_redraw();

		leadchar = 0;
		while(char_ok(leadchar) == FALSE){
			leadchar = (leadchar + 1);
		}
		rcty = (char_loc_y(leadchar) - 1);
		stepsnd = 0;
		while(rcty >= 55){
			march_party(32,rcty);
			force_view_center(32,rcty);
			force_instant_terrain_redraw();
			if(stepsnd == 0){
				play_sound(49);
				stepsnd = 1;
			}
			else{
				play_sound(50);
				stepsnd = 0;
			}
			pause(3);

			rcty = (rcty - 1);
		}

		reset_dialog();
		add_dialog_str(0,"...absolutely insane.",0);
		add_dialog_str(1,"The inside of the Red Fortress is just as imposing and darkly beautiful as the outside.  The walls are a drab black, stained by a crimson border and the inside is decorated with unearthly beasts carved from solid basalt.",0);
		add_dialog_str(2,"The back wall of this entryway is lined with Empire emblems, but also a more bizarre item-- two red stars flanking the inner gates.  What do they mean?",0);
		add_dialog_str(3,"The fortress itself appears to be quiet.  This is reassuring.  But up ahead...  it seems to be grassy?  Is The Immortal placing a grass field in his fort?  And if so, why?  It truly does seem to break the mood.",0);
		add_dialog_choice(0,"Strange...");
		bmessage = run_dialog(1);

		set_flag(15,1,2);
	}

	if(get_flag(15,1) == 3){
		activate_hidden_group(1);
		march_party(32,44);
		march_party(32,43);
		march_party(32,42);
		march_party(32,41);
		march_party(32,40);
		pcs = 6;
		while(pcs <= 37){
			set_character_pose(pcs,2);

			pcs = (pcs + 1);
		}
		force_view_center(32,40);
		force_instant_terrain_redraw();
		pause(10);

		reset_dialog();
		add_dialog_str(0,"The soldiers!  They're pouring out of the barracks on both sides.",0);
		add_dialog_str(1,"_Damnit!  You took too long!  Now look at what I've got to do._",0);
		add_dialog_str(2,"What SHE'S got to do?  Wait, what?",0);
		add_dialog_str(3,"_Yeah, you heard me!  I'm holding them off.  They don't want to kill you; I'm the one they're after.  Now run!_",0);
		add_dialog_choice(0,"Good luck, then.");
		bmessage = run_dialog(1);

		stepsnd = 0;
		rcty = 39;
		while(rcty >= 28){
			march_party(32,rcty);
			force_view_center(32,rcty);
			force_instant_terrain_redraw();
			if(stepsnd == 0){
				play_sound(49);
				stepsnd = 1;
			}
			else{
				play_sound(50);
				stepsnd = 0;
			}
			pause(2);

			rcty = (rcty - 1);
		}

		set_terrain(31,32,64);
		set_terrain(32,32,64);
		set_terrain(33,32,64);
		set_terrain(30,33,1);
		set_terrain(31,33,1);
		set_terrain(32,33,1);
		set_terrain(33,33,1);
		set_terrain(34,33,1);
		set_terrain(24,34,0);
		set_terrain(24,36,0);
		set_terrain(24,38,0);
		set_terrain(24,42,0);
		set_terrain(24,44,0);
		set_terrain(24,46,0);
		set_terrain(26,33,0);
		set_terrain(26,34,0);
		set_terrain(27,36,0);
		set_terrain(28,33,0);
		set_terrain(28,34,0);
		set_terrain(28,37,0);
		set_terrain(28,40,0);
		set_terrain(30,33,0);
		set_terrain(34,33,0);
		set_terrain(36,33,0);
		set_terrain(36,34,0);
		set_terrain(36,37,0);
		set_terrain(36,40,0);
		set_terrain(37,36,0);
		set_terrain(38,33,0);
		set_terrain(38,34,0);
		set_terrain(40,34,0);
		set_terrain(40,36,0);
		set_terrain(40,38,0);
		set_terrain(40,42,0);
		set_terrain(40,44,0);
		set_terrain(40,46,0);
		pcs = 6;
		while(pcs <= 37){
			erase_char(pcs);

			pcs = (pcs + 1);
		}
		rctx = 23;
		while(rctx <= 41){
			rcty = 33;
			while(rcty <= 47){
				set_floor(rctx,rcty,255);

				rcty = (rcty + 1);
			}
			rctx = (rctx + 1);
		}
		force_view_center(32,28);
		force_instant_terrain_redraw();
		play_sound(99);
		pause(8);

		text_bubble_on_char(38,"I told you that things");
		force_instant_terrain_redraw();
		pause(15);

		text_bubble_on_char(38,"");
		text_bubble_on_char(38,"would turn out like this.");
		force_instant_terrain_redraw();
		pause(16);

		text_bubble_on_char(38,"");
		text_bubble_on_char(39,"Silence, you senile fool!");
		force_instant_terrain_redraw();
		pause(16);

		text_bubble_on_char(39,"");
		text_bubble_on_char(38,"I'll take care of *her*.");
		force_instant_terrain_redraw();
		pause(16);

		text_bubble_on_char(38,"");
		text_bubble_on_char(38,"You have fun.");
		force_instant_terrain_redraw();
		pause(14);

		text_bubble_on_char(38,"");
		set_character_pose(38,1);
		force_instant_terrain_redraw();
		pause(1);

		erase_char(38);
		put_boom_on_space(31,22,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);
		pause(5);

		reset_dialog();
		add_dialog_str(0,"That was the wizard from the docks.  What is he doing here?  Nevermind that-- who's this man (just as likely a rakshasa) standing here now?",0);
		add_dialog_str(1,"_You!  The Immortal wishes to witness a duel between us.  My name is Profanus Pyre, though you may know me better as General Ignatius Astrophel.  Meet me upstairs, so that we may join in battle.  Do not disappoint me._",0);
		add_dialog_choice(0,"Okay, but...");
		bmessage = run_dialog(1);

		set_character_pose(39,1);
		force_instant_terrain_redraw();
		pause(1);

		erase_char(39);
		put_boom_on_space(33,25,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);
		pause(5);

		if(party_size() == 1)
			message_dialog("The sounds of the combat outside are inaudible.  Hunter should be doing okay, though I cannot count on her assistance in the battle with The Immortal's subordinate.","He told me to go upstairs...  Given the circumstances, I have no choice but.");
		if(party_size() > 1)
			message_dialog("The sounds of the combat outside are inaudible.  Hunter should be doing okay, though we cannot count on her assistance in the battle with The Immortal's subordinate.","He told us to go upstairs...  Given the circumstances, we have no choice but.");
		set_flag(15,1,4);
		block_entry(1);
	}

	if(get_flag(15,1) >= 4){
		set_terrain(31,32,64);
		set_terrain(32,32,64);
		set_terrain(33,32,64);
		set_terrain(30,33,1);
		set_terrain(31,33,1);
		set_terrain(32,33,1);
		set_terrain(33,33,1);
		set_terrain(34,33,1);
	}

	if((get_flag(15,1) >= 9) && (get_flag(15,1) <= 11)){
		set_terrain(24,7,268);
		set_terrain(32,7,268);
		set_terrain(40,7,268);
		set_terrain(24,15,268);
		set_terrain(32,15,268);
		set_terrain(40,15,268);
	}
	if(get_flag(15,1) == 9){
		message_dialog("There is a noticeable hum coming to the north.  That must be where the portals are.","");
		set_flag(15,1,10);
	}

break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;

	// SOLDIERS APPEARING
	if(get_flag(15,1) == 11){
		if((((get_flag(19,1) == 0) && (get_ran(1,0,1) == 1)) || ((get_flag(19,1) == 1) && (get_ran(1,0,3) < 3))) && (get_flag(25,1) < 34)){
			sld_typ = get_ran(1,0,8);
			if(sld_typ < 4)
				place_monster(24,8,6,0);
			if((sld_typ >= 4) && (sld_typ <= 6))
				place_monster(24,8,132,0);
			if(sld_typ > 6)
				place_monster(24,8,184,0);
			pcs = 86;
			telep_ok = 0;
			while((pcs <= 119) && (telep_ok == 0)){
				if(char_ok(pcs) == TRUE){
					if(get_stat(pcs,27) == 0)
						telep_ok = 1;
				}

				if(telep_ok == 0)
					pcs = (pcs + 1);
			}
			if(telep_ok == 1){
				alter_stat(pcs,27,1);
				rctx = char_loc_x(pcs);
				rcty = char_loc_y(pcs);
				put_boom_on_space(rctx,rcty,2,0);
				force_instant_terrain_redraw();
				run_animation_sound(10);

				inc_flag(25,1,1);
			}
		}
		if((((get_flag(19,1) == 0) && (get_ran(1,0,1) == 1)) || ((get_flag(19,1) == 1) && (get_ran(1,0,3) < 3))) && (get_flag(25,1) < 34)){
			sld_typ = get_ran(1,0,8);
			if(sld_typ < 4)
				place_monster(32,8,6,0);
			if((sld_typ >= 4) && (sld_typ <= 6))
				place_monster(32,8,132,0);
			if(sld_typ > 6)
				place_monster(32,8,184,0);
			pcs = 86;
			telep_ok = 0;
			while((pcs <= 119) && (telep_ok == 0)){
				if(char_ok(pcs) == TRUE){
					if(get_stat(pcs,27) == 0)
						telep_ok = 1;
				}

				if(telep_ok == 0)
					pcs = (pcs + 1);
			}
			if(telep_ok == 1){
				alter_stat(pcs,27,1);
				rctx = char_loc_x(pcs);
				rcty = char_loc_y(pcs);
				put_boom_on_space(rctx,rcty,2,0);
				force_instant_terrain_redraw();
				run_animation_sound(10);

				inc_flag(25,1,1);
			}
		}
		if((((get_flag(19,1) == 0) && (get_ran(1,0,1) == 1)) || ((get_flag(19,1) == 1) && (get_ran(1,0,3) < 3))) && (get_flag(25,1) < 34)){
			sld_typ = get_ran(1,0,8);
			if(sld_typ < 4)
				place_monster(40,8,6,0);
			if((sld_typ >= 4) && (sld_typ <= 6))
				place_monster(40,8,132,0);
			if(sld_typ > 6)
				place_monster(40,8,184,0);
			pcs = 86;
			telep_ok = 0;
			while((pcs <= 119) && (telep_ok == 0)){
				if(char_ok(pcs) == TRUE){
					if(get_stat(pcs,27) == 0)
						telep_ok = 1;
				}

				if(telep_ok == 0)
					pcs = (pcs + 1);
			}
			if(telep_ok == 1){
				alter_stat(pcs,27,1);
				rctx = char_loc_x(pcs);
				rcty = char_loc_y(pcs);
				put_boom_on_space(rctx,rcty,2,0);
				force_instant_terrain_redraw();
				run_animation_sound(10);

				inc_flag(25,1,1);
			}
		}
		if((((get_flag(19,1) == 0) && (get_ran(1,0,1) == 1)) || ((get_flag(19,1) == 1) && (get_ran(1,0,3) < 3))) && (get_flag(25,1) < 34)){
			sld_typ = get_ran(1,0,8);
			if(sld_typ < 4)
				place_monster(24,14,6,0);
			if((sld_typ >= 4) && (sld_typ <= 6))
				place_monster(24,14,132,0);
			if(sld_typ > 6)
				place_monster(24,14,184,0);
			pcs = 86;
			telep_ok = 0;
			while((pcs <= 119) && (telep_ok == 0)){
				if(char_ok(pcs) == TRUE){
					if(get_stat(pcs,27) == 0)
						telep_ok = 1;
				}

				if(telep_ok == 0)
					pcs = (pcs + 1);
			}
			if(telep_ok == 1){
				alter_stat(pcs,27,1);
				rctx = char_loc_x(pcs);
				rcty = char_loc_y(pcs);
				put_boom_on_space(rctx,rcty,2,0);
				force_instant_terrain_redraw();
				run_animation_sound(10);

				inc_flag(25,1,1);
			}
		}
		if((((get_flag(19,1) == 0) && (get_ran(1,0,1) == 1)) || ((get_flag(19,1) == 1) && (get_ran(1,0,3) < 3))) && (get_flag(25,1) < 34)){
			sld_typ = get_ran(1,0,8);
			if(sld_typ < 4)
				place_monster(32,14,6,0);
			if((sld_typ >= 4) && (sld_typ <= 6))
				place_monster(32,14,132,0);
			if(sld_typ > 6)
				place_monster(32,14,184,0);
			pcs = 86;
			telep_ok = 0;
			while((pcs <= 119) && (telep_ok == 0)){
				if(char_ok(pcs) == TRUE){
					if(get_stat(pcs,27) == 0)
						telep_ok = 1;
				}

				if(telep_ok == 0)
					pcs = (pcs + 1);
			}
			if(telep_ok == 1){
				alter_stat(pcs,27,1);
				rctx = char_loc_x(pcs);
				rcty = char_loc_y(pcs);
				put_boom_on_space(rctx,rcty,2,0);
				force_instant_terrain_redraw();
				run_animation_sound(10);

				inc_flag(25,1,1);
			}
		}
		if((((get_flag(19,1) == 0) && (get_ran(1,0,1) == 1)) || ((get_flag(19,1) == 1) && (get_ran(1,0,3) < 3))) && (get_flag(25,1) < 34)){
			sld_typ = get_ran(1,0,8);
			if(sld_typ < 4)
				place_monster(40,14,6,0);
			if((sld_typ >= 4) && (sld_typ <= 6))
				place_monster(40,14,132,0);
			if(sld_typ > 6)
				place_monster(40,14,184,0);
			pcs = 86;
			telep_ok = 0;
			while((pcs <= 119) && (telep_ok == 0)){
				if(char_ok(pcs) == TRUE){
					if(get_stat(pcs,27) == 0)
						telep_ok = 1;
				}

				if(telep_ok == 0)
					pcs = (pcs + 1);
			}
			if(telep_ok == 1){
				alter_stat(pcs,27,1);
				rctx = char_loc_x(pcs);
				rcty = char_loc_y(pcs);
				put_boom_on_space(rctx,rcty,2,0);
				force_instant_terrain_redraw();
				run_animation_sound(10);

				inc_flag(25,1,1);
			}
		}
	}

break;

beginstate 10;

	if(get_flag(15,1) < 3){
		if(is_combat() == TRUE){
			block_entry(1);
			end();
		}

		play_sound(95);
		if(party_size() == 1)
			message_dialog("The grass here...  it smells nice and natural.  It's deceptively alluring.  Why, if this wasn't the den of evil, I'd...  I'd take a nap.","That sounds nice.");
		if(party_size() > 1)
			message_dialog("The grass here...  it smells nice and natural.  It's deceptively alluring.  Why, if this wasn't the den of evil, we'd...  we'd take a nap.","That sounds nice.");

		move_to_new_town(23,18,18);
	}

break;

beginstate 11;

	move_to_new_town(24,26,23);

break;

beginstate 12;

	move_to_new_town(24,38,23);

break;

beginstate 13;

	if(get_flag(15,1) == 10){
		if(is_combat() == TRUE){
			block_entry(1);
			end();
		}

		reset_dialog();
		add_dialog_str(0,"Profanus Pyre!  He's already got a small cadre of soldiers by him, but with the portals open, he may well be liable to gain more in very short order.  He must be stopped.",0);
		add_dialog_choice(0,"But first, to prepare...");
		add_dialog_choice(1,"Charge!");
		bmessage = run_dialog(1);
		if(bmessage == 1){
			block_entry(1);
			end();
		}

		activate_hidden_group(2);
		stepsnd = 1;
		leadchar = 0;
		while(char_ok(leadchar) == FALSE){
			leadchar = (leadchar + 1);
		}
		if(char_loc_x(leadchar) < 32){
			if(char_loc_y(leadchar) > 12){
				march_party(18,12);
				force_view_center(18,12);
			}
			if(char_loc_y(leadchar) < 10){
				march_party(18,10);
				force_view_center(18,10);
			}
			if((char_loc_y(leadchar) <= 12) && (char_loc_y(leadchar) >= 10)){
				march_party(18,11);
				force_view_center(18,11);
			}
			force_instant_terrain_redraw();
			play_sound(49);
			pause(2);

			rctx = 19;
			while(rctx <= 27){
				pcs = 41;
				while(pcs <= 51){
					if(get_ran(1,0,3) == 1)
set_character_facing(pcs,2);

					pcs = (pcs + 1);
				}

				march_party(rctx,11);
				force_view_center(rctx,11);
				force_instant_terrain_redraw();
				if(stepsnd == 0){
					stepsnd = 1;
					play_sound(49);
				}
				else{
					stepsnd = 0;
					play_sound(50);
				}
				pause(2);

				rctx = (rctx + 1);
			}
			pcs = 41;
			while(pcs <= 51){
				set_character_facing(pcs,2);

				pcs = (pcs + 1);
			}
			force_instant_terrain_redraw();
			pause(3);

			set_character_facing(40,2);
			force_instant_terrain_redraw();
			pause(5);
		}

		if(char_loc_x(leadchar) > 32){
			if(char_loc_y(leadchar) > 12){
				march_party(46,12);
				force_view_center(46,12);
			}
			if(char_loc_y(leadchar) < 10){
				march_party(46,10);
				force_view_center(46,10);
			}
			if((char_loc_y(leadchar) <= 12) && (char_loc_y(leadchar) >= 10)){
				march_party(46,11);
				force_view_center(46,11);
			}
			force_instant_terrain_redraw();
			play_sound(49);
			pause(2);

			rctx = 45;
			while(rctx >= 37){
				pcs = 41;
				while(pcs <= 51){
					if(get_ran(1,0,3) == 1)
set_character_facing(pcs,6);

					pcs = (pcs + 1);
				}

				march_party(rctx,11);
				force_view_center(rctx,11);
				force_instant_terrain_redraw();
				if(stepsnd == 0){
					stepsnd = 1;
					play_sound(49);
				}
				else{
					stepsnd = 0;
					play_sound(50);
				}
				pause(2);

				rctx = (rctx - 1);
			}
			pcs = 41;
			while(pcs <= 51){
				set_character_facing(pcs,6);
				alter_stat(pcs,27,1);

				pcs = (pcs + 1);
			}
			force_instant_terrain_redraw();
			pause(3);

			set_character_facing(40,6);
			force_instant_terrain_redraw();
			pause(5);
		}

		reset_dialog();
		add_dialog_str(0,"_Ah, you're just in time..._",0);
		add_dialog_choice(0,"In time for what?");
		add_dialog_choice(1,"(Attack)");
		bmessage = run_dialog(0);
		if(bmessage == 1){
			set_flag(19,1,1);
			reset_dialog();
			add_dialog_str(0,"_You will be the first to witness the first battle of the officially-certified Red Army!_",0);
			add_dialog_str(1,"_Emperor Stewart has finally given General Rufus Rothgard a promotion.  Now, you may call him General Field Marshall Rothgard, Supreme Commander of the Red Army._",0);
			add_dialog_str(2,"_The Red Army is the elite force created to fight on the frontier against all threats to the Empire.  And here you are, the first threat it will be used to fight against._",0);
			add_dialog_str(3,"_These portals open up to all of the major fortresses across the Empire.  Truly, you will be swamped by the Empire's finest!  Take pride in this as you die._",0);
			add_dialog_str(4,"_Men!  For the glory of General Field Marshall Rothgard, attack!_",0);
		}
		else{
			reset_dialog();
			add_dialog_str(0,"_You will be the first to witness the first battle of the officially-certified Red Army!  Men!  For the glory of General Field Marshall Rothgard, attack!_",0);
		}
		add_dialog_choice(0,"You die today!");
		bmessage = run_dialog(1);

		rcty = 9;
		while(rcty <= 13){
			set_terrain(17,rcty,1);
			set_terrain(47,rcty,1);

			rcty = (rcty + 1);
		}
		set_character_facing(40,4);
		force_instant_terrain_redraw();

		block_entry(1);
		set_flag(15,1,11);
		set_incidental_sound(0);
	}

break;

